package com.cqk.bigdata.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author: cqk
 * @date: 2018/4/2 14:07
 * @param
*/
public class ProducerTest {
    public static void main(String[] args) {
        Properties kafkaProp = new Properties();
        kafkaProp.put("bootstrap.servers","spark001:9092");
        kafkaProp.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        kafkaProp.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        kafkaProp.put("client.id","consumer1——client");
        KafkaProducer<String,String> producer = new KafkaProducer<String,String>(kafkaProp);
        String topic = "kafka_test";
        ProducerRecord<String,String> record = new ProducerRecord<String, String>(topic,"key1","123");
        try{
            /*
             * 发送并忘记 不关心发送结果（成功失败）
             */
           // producer.send(record);
            /*
             * 同步发送消息
             */
            //producer.send(record).get();
            /*
             * 异步发消息 发消息时传递一个回调对象
             */


                producer.send(record,new DemoProducerCallback());
                Thread.sleep(1000);


        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
